package com.link510.aitools.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.EditText;

import com.blankj.utilcode.util.DeviceUtils;
import com.google.common.base.Strings;
import com.link510.aitools.R;
import com.link510.aitools.core.config.AppDefalutConfig;
import com.link510.aitools.core.domain.AppMachineInfo;
import com.link510.aitools.core.helper.TypeHelper;
import com.link510.aitools.framework.activity.BaseAcitivity;
import com.link510.aitools.services.Commons;

import butterknife.BindView;
import butterknife.OnClick;

public class SettingActivity extends BaseAcitivity {

    /**
     * 设备编号
     */
    @BindView(R.id.deviceSNView)
    EditText deviceSNView;


    /**
     * 设备名称
     */
    @BindView(R.id.deviceNameView)
    EditText deviceNameView;


    /**
     * 设备MAC
     */
    @BindView(R.id.deviceMacView)
    EditText deviceMacView;

    /**
     * 报警时长
     */
    @BindView(R.id.hostView)
    EditText hostView;

    /**
     * apiUrl地址
     */
    @BindView(R.id.portView)
    EditText portView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_setting);
        super.onCreate(savedInstanceState);
        initData();
    }

    @SuppressLint({"SetTextI18n", "MissingPermission"})
    private void initData() {

        AppMachineInfo machineInfo = Commons.getInstance().loadMachineInfo();

        if (machineInfo == null) {
            machineInfo = Commons.getInstance().loadMachineInfo();
        }

        deviceSNView.setText(machineInfo.getDeviceSN());
        deviceNameView.setText(machineInfo.getName());
        deviceMacView.setText(DeviceUtils.getMacAddress());
        portView.setText(machineInfo.getHost());
        hostView.setText(machineInfo.getPort().toString());
    }

    @SuppressLint("MissingPermission")
    @OnClick(R.id.saveSetting)
    public void saveMachine() {


        AppMachineInfo machineInfo = Commons.getInstance().loadMachineInfo();

        machineInfo.setDeviceSN(deviceSNView.getText().toString().trim());
        machineInfo.setName(deviceNameView.getText().toString().trim());
        machineInfo.setMac(DeviceUtils.getMacAddress());

        String host = portView.getText().toString().trim();
        if (Strings.isNullOrEmpty(host)) {
            host = AppDefalutConfig.getInstance().getHost();
        }
        machineInfo.setHost(host);

        Integer port = TypeHelper.getInstance()
                .stringToInt(hostView.getText().toString().trim(), AppDefalutConfig.getInstance().getPort());
        machineInfo.setPort(port);


        Commons.getInstance().saveMachineInfo(machineInfo);

        try {
            Thread.sleep(2000);
            /*杀死整个进程**/
            //AppUtils.exitApp();
            android.os.Process.killProcess(android.os.Process.myPid());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }

}
